<template>
  <div>
    <van-nav-bar title="标题" fixed>
      <template #title>
        <van-button type="info" to="/search">
          <template>
            <i class="toutiao toutiao-sousuo"></i>
            <span>搜索</span>
          </template>
        </van-button>
      </template>
    </van-nav-bar>

    <van-tabs v-model="active" animated>
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <article-list :channel="item"></article-list>
      </van-tab>
      <template #nav-right>
        <div class="tab-right" @click="editShow=true">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>

    <van-popup
    v-model="editShow"
    closeable
    close-icon="clear"
    position="bottom"
    close-icon-position="top-left"
    :style="{ height: '100%' }">
    <channel-edit :channels="channels" :activeIndex="active" @toggle-event="handleToggle"></channel-edit>
    </van-popup>
  </div>
</template>
<script>
import ArticleList from './components/article-list' /* eslint- */
import { getChannelList } from '@/api/user'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    'article-list': ArticleList,
    ChannelEdit
  },
  created () {
    this.getChannels()
  },
  data () {
    return {
      active: 0,
      channels: [],
      editShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    async getChannels () {
      try {
        if (this.user) { // 说明登录状态
          const { data: res } = await getChannelList()
          this.channels = res.data.channels
        } else { // 未登录
          // 从本地存储拿频道数据
          var list = getItem('TOUTIAO_CHANNELS')
          if (list) {
            this.channels = list
          } else {
            // 说明本地存储中没有频道数据
            const { data: res } = await getChannelList()
            this.channels = res.data.channels
          }
        }
      } catch (err) {
        this.$toast.fail('获取频道信息失败')
      }
    },
    handleToggle (index, flag) {
      this.active = index// 改变当前active索引
      this.editShow = flag // 关闭popup
    }
  }
}
</script>
<style scoped lang="less">
.van-nav-bar{

  /deep/ .van-nav-bar__title{
    // unset就是去掉自身的样式，并且继承父元素的样式 inherit
    max-width: unset;
  }
  .van-button{
    border-radius: 32px;
    background-color:rgba(255,255,255,0.2);
    width: 555px;
    height:64px;
    .toutiao{
      font-size:32px;
    }
    span{
      font-size:28px;
    }
  }
}

/deep/ .van-tab{
  min-width:192px;
}

::v-deep .van-tabs__line{
    width: 31px;
    height: 6px;
    background-color: #3296fa;
    border-radius: 3px;
  }

.tab-right{
  &::after{
    content:"";
    width:10px;
    height:58px;
    background:blue;
    flex-shrink:0;
    position: absolute;
    left:0;
    background:url(~@/assets/gradient-gray-line.png) no-repeat;
  }
  width: 66px;
  height: 82px;
  background-color: rgba(255,255,255,0.9);
  position: fixed;
  right:0;
  display: flex;
  align-items: center;
  justify-content: center;
  .toutiao{
    font-size:33px;
  }

}

/deep/ .van-tabs__wrap{
  // 防止最右边被挡住
  padding-right:66px;
  position: fixed;
  top:92px;
  left:0;
  // 宽度就是100%
  right:0;
  z-index:2;
}
/deep/ .van-tabs__content{
  padding-top:200px;
  z-index:1;
  // vh是相对单位，视口的高度算一百份，1vh就是一份
  // 视口的宽度是100份，1vw就是占一份
  // height:79vh;
  // // 超出了79vh的话，就会出现滚动条
  // overflow-y: auto;
}
</style>
